﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shared
{
    // 所有函数异常返回empty(比如"", byte[]), 不返回null
    class Rc4LikedEncrypt
    {
        private static Random random = new Random((int)DateTime.Now.Ticks);
        private static byte[] encode_char_map = new byte[] {
            0x84, 0xc0, 0x02, 0x9b, 0xbf, 0xd2, 0x09, 0x0a, 0x5b, 0xee, 0x51, 0x42, 0x30, 0x68, 0x6f, 0x95,
            0x4e, 0x46, 0x7a, 0x99, 0x74, 0x58, 0x00, 0x63, 0x50, 0x4a, 0x36, 0xea, 0x34, 0xb6, 0xdf, 0xa7,
            0xc1, 0xcc, 0xb8, 0x3d, 0xab, 0x7c, 0xfc, 0xce, 0x8a, 0xb4, 0xb9, 0xbe, 0x70, 0xa0, 0x90, 0xcf,
            0x65, 0xca, 0x24, 0x48, 0xd1, 0x98, 0x43, 0x6a, 0x7b, 0x5d, 0x9c, 0x3b, 0x2a, 0x49, 0x52, 0x89,
            0x4f, 0x5f, 0xf7, 0x31, 0x25, 0x93, 0x33, 0xd0, 0x81, 0xb0, 0xff, 0xb5, 0xde, 0x7e, 0x73, 0x86,
            0x1f, 0xe6, 0x2b, 0x27, 0x19, 0x2d, 0x7f, 0xcb, 0x41, 0x7d, 0x9a, 0x8c, 0x60, 0xc8, 0xe9, 0x10,
            0x44, 0xe0, 0x72, 0xa1, 0x87, 0x55, 0x57, 0x16, 0x9f, 0x54, 0x17, 0xbb, 0x08, 0xc4, 0x05, 0x18,
            0x29, 0xc5, 0x3c, 0x97, 0xe3, 0x20, 0x21, 0x69, 0x8d, 0x8b, 0xf8, 0x47, 0x1c, 0x77, 0x56, 0x03,
            0xeb, 0x0d, 0x92, 0x82, 0xd8, 0x78, 0x8e, 0x12, 0xb2, 0x07, 0x8f, 0xad, 0x61, 0x06, 0x5a, 0x2e,
            0xae, 0x79, 0x15, 0x85, 0xd6, 0xe1, 0xa8, 0x4d, 0xf3, 0x39, 0xb3, 0x88, 0xac, 0x28, 0xfb, 0x11,
            0xd9, 0xa9, 0x04, 0x71, 0xa6, 0xf4, 0x38, 0xe4, 0x6b, 0x32, 0xa4, 0xd5, 0x96, 0x22, 0x0e, 0xe7,
            0x5c, 0x6d, 0x2c, 0x4b, 0xd3, 0x01, 0xfd, 0xb7, 0x9d, 0xa3, 0xcd, 0x94, 0xef, 0xc2, 0x66, 0xa5,
            0x64, 0xc6, 0x62, 0xf9, 0xdb, 0xda, 0xdd, 0xa2, 0x76, 0x0c, 0xe8, 0x3e, 0x0f, 0xe5, 0x1b, 0x45,
            0xf1, 0x5e, 0xbc, 0x6e, 0xaf, 0x14, 0x59, 0xaa, 0xfe, 0x1a, 0x35, 0xf6, 0x23, 0x6c, 0x80, 0xdc,
            0xb1, 0xe2, 0xf0, 0x53, 0x83, 0xfa, 0xbd, 0x2f, 0xec, 0x91, 0xf5, 0x40, 0x4c, 0x37, 0xd7, 0xd4,
            0x9e, 0x3a, 0xf2, 0x0b, 0x1e, 0x75, 0xed, 0x26, 0xc3, 0xc9, 0x67, 0xc7, 0xba, 0x1d, 0x13, 0x3f,
        };
        private static byte[] decode_char_map = new byte[] {
            0x16, 0xb5, 0x02, 0x7f, 0xa2, 0x6e, 0x8d, 0x89, 0x6c, 0x06, 0x07, 0xf3, 0xc9, 0x81, 0xae, 0xcc,
            0x5f, 0x9f, 0x87, 0xfe, 0xd5, 0x92, 0x67, 0x6a, 0x6f, 0x54, 0xd9, 0xce, 0x7c, 0xfd, 0xf4, 0x50,
            0x75, 0x76, 0xad, 0xdc, 0x32, 0x44, 0xf7, 0x53, 0x9d, 0x70, 0x3c, 0x52, 0xb2, 0x55, 0x8f, 0xe7,
            0x0c, 0x43, 0xa9, 0x46, 0x1c, 0xda, 0x1a, 0xed, 0xa6, 0x99, 0xf1, 0x3b, 0x72, 0x23, 0xcb, 0xff,
            0xeb, 0x58, 0x0b, 0x36, 0x60, 0xcf, 0x11, 0x7b, 0x33, 0x3d, 0x19, 0xb3, 0xec, 0x97, 0x10, 0x40,
            0x18, 0x0a, 0x3e, 0xe3, 0x69, 0x65, 0x7e, 0x66, 0x15, 0xd6, 0x8e, 0x08, 0xb0, 0x39, 0xd1, 0x41,
            0x5c, 0x8c, 0xc2, 0x17, 0xc0, 0x30, 0xbe, 0xfa, 0x0d, 0x77, 0x37, 0xa8, 0xdd, 0xb1, 0xd3, 0x0e,
            0x2c, 0xa3, 0x62, 0x4e, 0x14, 0xf5, 0xc8, 0x7d, 0x85, 0x91, 0x12, 0x38, 0x25, 0x59, 0x4d, 0x56,
            0xde, 0x48, 0x83, 0xe4, 0x00, 0x93, 0x4f, 0x64, 0x9b, 0x3f, 0x28, 0x79, 0x5b, 0x78, 0x86, 0x8a,
            0x2e, 0xe9, 0x82, 0x45, 0xbb, 0x0f, 0xac, 0x73, 0x35, 0x13, 0x5a, 0x03, 0x3a, 0xb8, 0xf0, 0x68,
            0x2d, 0x63, 0xc7, 0xb9, 0xaa, 0xbf, 0xa4, 0x1f, 0x96, 0xa1, 0xd7, 0x24, 0x9c, 0x8b, 0x90, 0xd4,
            0x49, 0xe0, 0x88, 0x9a, 0x29, 0x4b, 0x1d, 0xb7, 0x22, 0x2a, 0xfc, 0x6b, 0xd2, 0xe6, 0x2b, 0x04,
            0x01, 0x20, 0xbd, 0xf8, 0x6d, 0x71, 0xc1, 0xfb, 0x5d, 0xf9, 0x31, 0x57, 0x21, 0xba, 0x27, 0x2f,
            0x47, 0x34, 0x05, 0xb4, 0xef, 0xab, 0x94, 0xee, 0x84, 0xa0, 0xc5, 0xc4, 0xdf, 0xc6, 0x4c, 0x1e,
            0x61, 0x95, 0xe1, 0x74, 0xa7, 0xcd, 0x51, 0xaf, 0xca, 0x5e, 0x1b, 0x80, 0xe8, 0xf6, 0x09, 0xbc,
            0xe2, 0xd0, 0xf2, 0x98, 0xa5, 0xea, 0xdb, 0x42, 0x7a, 0xc3, 0xe5, 0x9e, 0x26, 0xb6, 0xd8, 0x4a,
        };

        public static byte EncodeByte(byte ib, int randomValue)
        {
            int newIndex = (ib + randomValue) & 0xFF;
            return encode_char_map[newIndex];
        }

        public static byte DecodeByte(byte ib, int randomValue)
        {
            int midChar = decode_char_map[ib];
            return (byte)((midChar + 256 - randomValue) & 0xFF);
        }

        public static byte[] EncodeBytes(byte[] input)
        {
            int randomValue = random.Next(0, 0xFF);
            byte[] output = new byte[input.Length + 1];
            output[0] = (byte)randomValue;
            for (int i = 0; i < input.Length; i ++)
            {
                output[i + 1] = EncodeByte(input[i], randomValue);
            }
            return output;
        }


        public static byte[] DecodeBytes(byte[] input)
        {
            try
            {
                byte[] output = new byte[input.Length - 1];
                int randomValue = input[0];
                for (int i = 0; i < output.Length; i ++)
                {
                    output[i] = DecodeByte(input[i + 1], randomValue);
                }
                return output;
            }
            catch(Exception e)
            {
                Debug.WriteLine(e);
                return new byte[0];
            }
        }

        private static String ToBCDEncode(byte[] input)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in input)
            {
                string hexOutput = String.Format("{0:X2}", (int)b);
                sb.Append(hexOutput);
            }
            return sb.ToString();
        }

        private static byte[] ToBCDDecode(String input)
        {
            byte[] output = new byte[input.Length / 2];
            for (int i = 0; i < input.Length/2; i ++)
            {
                int hex = Convert.ToInt32(input.Substring(i * 2, 2), 16);
                output[i] = (byte)hex;
            }
            return output;
        }

        public static String EncodeString(String input)
        {
            try
            {
                byte[] inputBytes = Encoding.UTF8.GetBytes(input);
                byte[] outputBytes = EncodeBytes(inputBytes);
                String output = ToBCDEncode(outputBytes);
                return output;
            }
            catch(Exception e)
            {
                Debug.WriteLine(e);
                return "";
            }
        }


        public static String DecodeString(String input)
        {
            try
            {
                byte[] outputBytes = ToBCDDecode(input);
                byte[] decodedBytes = DecodeBytes(outputBytes);
                String output = Encoding.UTF8.GetString(decodedBytes);
                return output;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return "";
            }
        }

        public class Rc4LikedEncryptChain
        {
            private readonly int randomValue = 0;
            private int lastOb = 0;
            private int handledCount = 0;
            public Rc4LikedEncryptChain(int randomValue)
            {
                this.randomValue = randomValue;
            }

            public void EncodeInplace(byte[] inputBytes)
            {
                EncodeInplace(inputBytes, inputBytes.Length);
            }

            public void EncodeInplace(byte[] inputBytes, int want_encode_length)
            {                
                for (int index = 0; index < want_encode_length; index ++)
                {
                    byte ib = inputBytes[index];
                    ib = (byte)(((int)ib + lastOb + handledCount) & 0xFF);
                    byte ob = Rc4LikedEncrypt.EncodeByte(ib, randomValue);
                    lastOb = ob;
                    handledCount++;
                    inputBytes[index] = ob;
                }
            }

            public void DecodeInplace(byte[] inputBytes)
            {
                DecodeInplace(inputBytes, inputBytes.Length);
            }

            public void DecodeInplace(byte[] inputBytes, int want_decode_length)
            {
                for (int index = 0; index < want_decode_length; index++)
                {
                    byte ib = inputBytes[index];
                    byte ob = Rc4LikedEncrypt.DecodeByte(ib, randomValue);
                    ob = (byte)(((int)ob + 256 - lastOb - handledCount) & 0xFF);
                    lastOb = ib;
                    handledCount++;
                    inputBytes[index] = ob;
                }
            }
        }
    };      
    
}
